{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Regularization\n",
    "\n",
    "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/geoai/blob/main/docs/examples/regularization.ipynb)\n",
    "\n",
    "This example demonstrates how to regularize building footprints using the `regularize` function from the `geoai` package. The function is a wrapper around the `regularize_geodataframe` function from the [buildingregulariser](https://github.com/DPIRD-DMA/Building-Regulariser) package. Credits to the original author, Nick Wright.\n",
    "\n",
    "It can be used to regularize features such as building footprints, solar panels, cars, and other objects that have a regular shape.\n",
    "\n",
    "## Install package\n",
    "To use the `geoai-py` package, ensure it is installed in your environment. Uncomment the command below if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install geoai-py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import geoai"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Use sample data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "raster_url = (\n",
    "    \"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip_train.tif\"\n",
    ")\n",
    "vector_url = \"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/buildings_original.geojson\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualize data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_vector_interactive(vector_url, tiles=raster_url)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Regularize buildings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = geoai.regularize(\n",
    "    data=vector_url,\n",
    "    simplify_tolerance=2.0,\n",
    "    allow_45_degree=True,\n",
    "    diagonal_threshold_reduction=30,\n",
    "    allow_circles=True,\n",
    "    circle_threshold=0.9,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_vector_interactive(gdf, tiles=raster_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.create_split_map(\n",
    "    left_layer=gdf,\n",
    "    right_layer=raster_url,\n",
    "    left_label=\"Regularized Buildings\",\n",
    "    right_label=\"NAIP Imagery\",\n",
    "    left_args={\"style\": {\"color\": \"red\", \"fillOpacity\": 0.3}},\n",
    "    basemap=raster_url,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Compare results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap.foliumap as leafmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m.add_cog_layer(raster_url, name=\"NAIP\")\n",
    "m.add_vector(\n",
    "    vector_url, style={\"color\": \"yellow\", \"fillOpacity\": 0}, layer_name=\"Original\"\n",
    ")\n",
    "m.add_gdf(gdf, style={\"color\": \"red\", \"fillOpacity\": 0}, layer_name=\"Regularized\")\n",
    "legend = {\n",
    "    \"Original\": \"yellow\",\n",
    "    \"Regularized\": \"red\",\n",
    "}\n",
    "m.add_legend(title=\"Building Footprints\", legend_dict=legend)\n",
    "m"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "geo",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
